package org.wotou.authority.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.List;

/**
 * Jpa返回分页查询
 *
 * @author zhangQ
 * @date 2017/12/7
 */
public class PageJpa<T> {

    /**
     * 当前页
     */
    private int pageNumber;
    /**
     * 页码
     */
    private int pageSize;
    /**
     * 排序名称
     */
    private String sortName;
    /**
     * 排序方式
     */
    private String sortOrder;
    private T obj;
    /**
     * 总条数
     */
    private long total;
    /**
     * 行数
     */
    private List<T> rows;

    @JsonIgnore
    public PageRequest getPageable() {
        PageRequest pageable = null;
        if (StringUtils.isEmpty(sortName) == false) {
            pageable = new PageRequest(pageNumber - 1,
                    pageSize,
                    "desc".equals(sortOrder) ? Sort.Direction.DESC : Sort.Direction.ASC, sortName);
        } else {
            pageable = new PageRequest(pageNumber - 1,
                    pageSize);
        }
        return pageable;
    }

    @JsonIgnore
    public <E> PageJpa<E> getData(Class clazz, Method method) throws Exception {
        PageJpa<E> page = new PageJpa<E>();
        Page<E> p = (Page<E>)method.invoke(clazz, this.getPageable());
        if (p != null) {
            page.total = p.getTotalElements();
            page.rows = p.getContent();
        }
        return page;
    }


    public T getObj() {
        return obj;
    }

    public void setObj(T obj) {
        this.obj = obj;
    }

    public long getTotal() {
        return total;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }


    public int getPageNumber() {
        return pageNumber;
    }

    public int getPageSize() {
        return pageSize;
    }

    public String getSortName() {
        return sortName;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    @Override
    public String toString() {
        return "PageJpa [pageNumber=" + pageNumber + ", pageSize=" + pageSize + ", sortName=" + sortName
                + ", sortOrder=" + sortOrder + ", obj=" + obj + ", total=" + total + ", rows=" + rows + "]";
    }
}
